package com.cloudweb.oa.expr;

import cn.js.fan.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.vo.Result;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.flow.FormField;
import com.redmoon.oa.flow.FormParser;
import com.redmoon.oa.visual.FormDAO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@RestController
@RequestMapping("/expr")
public class ExprController {

    @ApiOperation(value = "列出公式", notes = "列出公式")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    public Result<Object> list() {
        JSONArray ary = new JSONArray();
        FormDAO dao = new FormDAO();
        FormDb fd = new FormDb();
        fd = fd.getFormDb("expression");
        FormField ff = fd.getFormField("kind");
        String[][] arr = FormParser.getOptionsArrayOfSelect(fd, ff);
        for (String[] item : arr) {
            JSONObject json = new JSONObject();
            json.put("label", item[0]);
            json.put("value", item[1]);
            JSONArray arrChild = new JSONArray();
            Map<String, Object> params = new HashMap<>();
            params.put("kind", item[1]);
            List<FormDAO> list = dao.selectList("expression", params);
            for (FormDAO fdao : list) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", fdao.getFieldValue("code"));
                jsonObject.put("label", fdao.getFieldValue("name"));
                int type = StrUtil.toInt(fdao.getFieldValue("data_type"), FormField.FIELD_TYPE_VARCHAR);
                jsonObject.put("type", type);
                jsonObject.put("typeDesc", FormField.getFieldTypeDesc(type));
                jsonObject.put("kind", fdao.getFieldValue("kind"));
                jsonObject.put("kindTitle", FormParser.getOptionText(arr, fdao.getFieldValue("kind")));
                jsonObject.put("description", fdao.getFieldValue("description"));
                arrChild.add(jsonObject);
            }
            json.put("children", arrChild);
            ary.add(json);
        }
        return new Result<>(ary);
    }

    @ApiOperation(value = "列出公式", notes = "列出公式")
    @ResponseBody
    @RequestMapping(value = "/getFormsAndFields", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    public Result<Object> getFormsAndFields(@RequestParam String formCode) {
        JSONArray ary = new JSONArray();
        FormDb fd = new FormDb();
        fd = fd.getFormDb(formCode);
        JSONObject json = new JSONObject();
        json.put("value", fd.getCode());
        json.put("label", fd.getName());

        Vector<FormField> vField = fd.getFields();
        JSONArray arrChild = new JSONArray();
        for (FormField ff : vField) {
            JSONObject jsonField = new JSONObject();
            jsonField.put("label", ff.getTitle());
            jsonField.put("value", ff.getName());
            jsonField.put("fieldType", ff.getFieldType());
            jsonField.put("fieldTypeDesc", ff.getFieldTypeDesc());
            arrChild.add(jsonField);
        }
        json.put("children", arrChild);
        ary.add(json);

        Vector<FormDb> v = fd.list();
        for (FormDb formDb : v) {
            if (!formDb.getCode().equals(formCode)) {
                json = new JSONObject();
                json.put("value", formDb.getCode());
                json.put("label", formDb.getName());

                vField = formDb.getFields();
                arrChild = new JSONArray();
                for (FormField ff : vField) {
                    JSONObject jsonField = new JSONObject();
                    jsonField.put("label", ff.getTitle());
                    jsonField.put("value", ff.getName());
                    jsonField.put("fieldType", ff.getFieldType());
                    arrChild.add(jsonField);
                }
                json.put("children", arrChild);
                ary.add(json);
            }
        }
        ary.add(json);
        return new Result<>(ary);
    }
}
